﻿@page "/authentications/idproviders/{id}/{action}"
@layout SimpleIdServer.IdServer.Website.Shared.MainLayout
@attribute [Authorize("Authenticated")]
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<IdProviderState> idProviderState
@inject NotificationService notificationService
@inject NavigationManager navigationManager
@inject IDispatcher dispatcher
@inject IUrlHelper urlHelper

<RadzenBreadCrumb>
    <RadzenBreadCrumbItem Path="@urlHelper.GetUrl("/authentications/idproviders")" Text="@Global.IdentityProviders" />
    <RadzenBreadCrumbItem Text="@Global.IdentityProviderDetails" />
</RadzenBreadCrumb>

<RadzenText class="mt-3 mb-3" Text="@idProviderState.Value.Provider?.DisplayName" TextStyle="TextStyle.DisplayH3" />

<RadzenCardLoader IsLoading=@idProviderState.Value.IsLoading Variant="Variant.Filled">
    <RadzenTabs Change=@OnChange @bind-SelectedIndex="selectedIndex">
        <Tabs>            
            <RadzenTabsItem Text="@Global.Details">
                <IdProviderDetails IdProvider="@idProviderState.Value.Provider"></IdProviderDetails>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Properties">
                <IdProviderProperties IdProvider="@idProviderState.Value.Provider"></IdProviderProperties>
            </RadzenTabsItem>
            <RadzenTabsItem Text="@Global.Mappers">
                <IdProviderMappers IdProvider="@idProviderState.Value.Provider"></IdProviderMappers>
            </RadzenTabsItem>
        </Tabs>
    </RadzenTabs>
</RadzenCardLoader>

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "details", 0 },
        { "properties", 1 },
        { "mappers", 2 }
    };

    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string action { get; set; }
    int selectedIndex = 0;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<GetIdProviderFailureAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                StateHasChanged();
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(id)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action];
        dispatcher.Dispatch(new GetIdProviderAction { Id = id });
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
        navigationManager.NavigateTo(urlHelper.GetUrl($"/authentications/idproviders/{id}/{rec.Key}"));
    }
}